package com.university.report.cmn.handler;

import com.alibaba.fastjson.JSON;
import org.springframework.data.redis.core.RedisTemplate;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName health_report
 * @Author Jinondo
 * @Date 2021/6/20 21:49
 */
public class ImpRegionPipeline implements Pipeline {

    private final RedisTemplate redisTemplate3;

    public ImpRegionPipeline(RedisTemplate redisTemplate3) {
        this.redisTemplate3 = redisTemplate3;
    }

    @Override
    public void process(ResultItems resultItems, Task task) {
        List<List<String>> importantRegions = resultItems.get("importantRegions");
        saveToRedis(importantRegions);
    }

    public void saveToRedis(List<List<String>> importantRegions){
        String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis()));
        String value = JSON.toJSONString(importantRegions);
        redisTemplate3.opsForValue().set("ImportantRegion:"+format,value,1, TimeUnit.DAYS);
    }
}
